<?php
/**
 * Created by PhpStorm.
 * User: 17628
 * Date: 2021-11-04
 * Time: 9:20
 */

namespace app\api\controller;


use app\common\controller\Api;
use app\admin\model\Integral as IntegralModel;
class Integral extends Api
{
    /**
     *
     * 我的积分显示
     */
    public function index()
    {
        $score = $this->auth->score;
        $page = input('page', 1);
        $type = input('type', '');
        $where = [];
        if (!empty($type)) {
            $where['type'] = ['=', $type];
        }
        $score_log = IntegralModel::where(['user_id' => $this->auth->id])
            ->field('id,memo,score,type,status_switch,createtime')
            ->where($where)
            ->page($page, 10)
            ->select();
        $count = IntegralModel::where(['user_id' => $this->auth->id])
            ->where($where)
            ->count();
        $maxpage = ceil($count / 10);
        foreach ($score_log as $v) {
            if ($v['status'] == 1) {
                $v['score'] = '+' . $v['score'];
            } else {
                $v['score'] = '-' . $v['score'];
            }
            $v['createtime'] = date('Y-m-d H:i:s', $v['createtime']);
        }
        $data = ['score' => $score, 'score_log' => $score_log, 'count' => $count, 'maxpage' => $maxpage];
        $this->success('获取成功', $data);
    }
}